package com.clear.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.clear.auth.common.entity.constants.AuthConstant;
import com.clear.auth.common.enums.AuthPermissionShowEnum;
import com.clear.auth.common.enums.AuthPermissionStatusEnum;
import com.clear.auth.common.enums.IsDeletedFlagEnum;
import com.clear.auth.domain.convert.AuthPermissionConverter;
import com.clear.auth.domain.entity.AuthPermissionBO;
import com.clear.auth.domain.redis.RedisUtil;
import com.clear.auth.domain.service.AuthPermissionDomainService;
import com.clear.auth.infra.basic.entity.AuthPermission;
import com.clear.auth.infra.basic.service.AuthPermissionService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增权限
     *
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.add.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBOToEntity(authPermissionBO);

        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        authPermission.setStatus(AuthPermissionStatusEnum.OPEN.getCode());  // 新增的权限默认 启用
        authPermission.setShow(AuthPermissionShowEnum.SHOW.getCode());  // 新增的权限默认 展示

        int count = authPermissionService.insert(authPermission);
        return count > 0;
    }

    /**
     * 更新权限
     *
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.update.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBOToEntity(authPermissionBO);

        int count = authPermissionService.update(authPermission);

        return count > 0;
    }

    /**
     * 删除权限
     *
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.delete.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBOToEntity(authPermissionBO);

        // 逻辑删除
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = authPermissionService.update(authPermission);

        return count > 0;
    }

    /**
     * 查询用户权限
     *
     * @param username
     * @return
     */
    @Override
    public List<String> getPermission(String username) {
        log.info("AuthPermissionDomainServiceImpl.getPermission.username:{}", username);

        String permissionKey = redisUtil.buildKey(AuthConstant.AUTH_PERMISSION_PREFIX, username);
        String permissionValue = redisUtil.get(permissionKey);
        // 权限值为空，直接返回空权限列表
        if (StringUtils.isBlank(permissionValue)) {
            return Collections.emptyList();
        }
        // 不为空，序列化权限值
        List<AuthPermission> authPermissionList = new Gson().fromJson(permissionValue, new TypeToken<List<AuthPermission>>() {
        }.getType());
        List<String> permissionList = authPermissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());

        return permissionList;
    }

}
